package web.servlet;

import domain.Account;
import domain.Cart;
import domain.Order;
import service.CartService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class NewOrderFormServlet extends HttpServlet {

    private static final String NEW_ORDER_FORM = "/WEB-INF/jsp/order/newOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        Cart cart=(Cart) session.getAttribute("cart");
        //CartService cartService=new CartService();
        if(loginAccount == null){
            resp.sendRedirect("signOnForm");
        }else{
            Order order=new Order();
            order.initOrder(loginAccount,cart);
            session.setAttribute("order",order);
            //在请求转发之前要先new出order对象，拿到account和cart里面的值存进去，做好准备
            req.getRequestDispatcher(NEW_ORDER_FORM).forward(req, resp);
        }
    }
}
